﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class ShopDAL
    {
        public ShopDAL(HoteContext hoteContext)
        {
            HoteContext = hoteContext;
        }

        public HoteContext HoteContext { get; }
        /// <summary>
        /// 添加门店
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int ShopAdd(ShopModel info)
        {
            try
            {
                int res = HoteContext.shopModels.Count(o => o.ShopName == info.ShopName);
                if (res > 0)
                {
                    return -1;//门店名称重复
                }
                HoteContext.shopModels.Add(info);
                return HoteContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int ShopDelete(int id)
        {
            try
            {
                var res = HoteContext.shopModels.Find(id);
                if (res!=null)
                {
                    res.IdDelete = true;
                }
                return HoteContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int ShopDeleteAll(string ids)
        {
            try
            {
                var list = ids.Split(',');
                foreach (var id in list)
                {
                    var info=HoteContext.shopModels.Find(Convert.ToInt32(id));
                    info.IdDelete= true;
                }
                return HoteContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 批量修改状态
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public int ShopUpdateAll(string ids,int state)
        {
            try
            {
               var idsAll=ids.Split(",");
                foreach (var id in idsAll)
                {
                    var info = HoteContext.shopModels.Find(Convert.ToInt32(id));
                    info.State = state;
                }
                return HoteContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// id查询(反填)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ShopModel GetShopId(int id)
        {
            try
            {
                return HoteContext.shopModels.Find(id);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改门店信息
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int ShopUpdate(ShopModel info)
        {
            try
            {
                var res = HoteContext.shopModels.Count(o => o.IdDelete == false && o.ShopName == info.ShopName && o.Id != info.Id);
                if (res > 0) 
                { 
                    return -1;
                }
                HoteContext.shopModels.Update(info); 
                return HoteContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示查询分页
        /// </summary>
        /// <param name="totalCount"></param>
        /// <param name="totalPage"></param>
        /// <param name="pageindex"></param>
        /// <param name="pageSize"></param>
        /// <param name="ShopName"></param>
        /// <param name="Scope"></param>
        /// <returns></returns>
        public List<ShopModel> GetShopList(out int totalCount,out int totalPage,int pageindex=1,int pageSize=2,string? ShopName="", int? Scope = null)
        {
            try
            {
                var list = HoteContext.shopModels.Where(o => o.IdDelete == false).ToList();
                if (!string.IsNullOrEmpty(ShopName))
                {
                    list = list.Where(o => o.ShopName.Contains(ShopName)).ToList();
                }
                if (Scope!=null)
                {
                    list = list.Where(o => o.Scope == Scope).ToList();
                }
                totalCount = list.Count();
                totalPage = (int)Math.Ceiling(totalCount * 1.0 / pageSize);
                list=list.OrderByDescending(o => o.Id).Skip((pageindex-1)* pageSize).Take(pageSize).ToList();
                return list;

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
